<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #############################################################################################
    # Programmer: นวัตพล ยศกำธร     						                        #
    # Email : golf.enfc40@gmail.com								#
    # Website : http://www.nawattapon.com							#
    #############################################################################################
    
    require_once(dirname(__FILE__).'/facebook/facebook.php');
    
    class Myfacebook extends Facebook
    {
        private $facebook_config = array();
        private $facebook_porfile = array();
        
        public function __construct()
        {
            $this->CI =& get_instance();
            $this->CI->config->load('facebook_config', TRUE);
            $this->facebook_config = $this->CI->config->item('facebook_config');
            
            /***********
            REQUEST
            ***********/
            $redirect = empty($_REQUEST['redirect']) ? "" : $_REQUEST['redirect'];
            $m = empty($_REQUEST['m']) ? "" : $_REQUEST['m'];
            $request = empty($_REQUEST['request_ids']) ? "" : $_REQUEST['request_ids'];
            
            /* **********
            redirect to Canvas Page
            ********** */
            //if (strpos($_SERVER['HTTP_REFERER'], $this->facebook_config['fb_canvas_url']) === false) {	
            //    //echo '<script type="text/javascript">top.location.href = "' . $this->facebook_config['fb_canvas_url'] . '";</script>';
            //    //exit;
            //}
            
            /* **********
            redirect after permission is allow
            ********** */
            if (!empty($redirect) && $redirect == "true") {
                echo '<script type="text/javascript">top.location.href = "' . $this->facebook_config['fb_tab_url'] . '";</script>';
                exit;
            }
            
            /* **********
            redirect if not use https
            ********** */
            if (empty($_SERVER['HTTPS'])) {
                //echo '<script type="text/javascript">top.location.href = "' . $this->facebook_config['fb_canvas_url'] . '";</script>';
                //exit;
            }
            
            $fb_config = array(
                'appId'  => $this->facebook_config['fb_app_id'],
                'secret' => $this->facebook_config['fb_app_secret'],
                'fileUpload' => $this->facebook_config['fb_file_upload']
            );
            
            parent::__construct($fb_config);
            
            if( ! $this->getUser()){
                $this->facebook_connect();
            }
        }
        
        private function facebook_connect()
        {
            $user = $this->getUser();
            
            if ($user) {
                try {
                    /* **********
                    facebook user information
                    ********** */
                    $this->set_facebook_profile($this->api('/me'));
                    
                    $friendAppUsersQuery = array("method" => "friends.getAppUsers", "uids" => $this->facebook_porfile['id']);
                    $friendAppUsers = $this->api($friendAppUsersQuery);
                    $friendAppUsers = implode(",", $friendAppUsers);
                    
                    $_SESSION['friendInApp'] = $friendAppUsers;
                    
                    // check if request_ids is not empty		
                    if (!empty($request)) {
                        try {
                            // do here
                            $reqs = explode("," , $request);
                            foreach($reqs as $id => $value) {
                                $req = $this->api("/$value/");
                            }
                                
                        } catch (FacebookApiException $e) {
                            //print_r($e);
                        }
                    }
                } catch (FacebookApiException $e) {
                    error_log($e);
                    $user = null;
                }
            } else {
                $params = array(
                    'scope' => $this->facebook_config['fb_scope'],
                    'redirect_uri' => $this->facebook_config['fb_canvas_url'] . '?redirect=true'
                );
                
                /* **********
                permission information : http://developers.facebook.com/docs/authentication/permissions/
                ********** */
                $redirect = $this->getLoginUrl($params);
                //echo $redirect;exit;
                echo '<script type="text/javascript">top.location.href = "' . $redirect . '";</script>';
                exit;
            }
        }
        
        private function set_facebook_profile($facebook_porfile)
        {
            $this->facebook_porfile = $facebook_porfile;
        }
        
        public function get_facebook_profile()
        {
            if(empty($this->facebook_porfile)){
                $this->facebook_connect();
            }
            return $this->facebook_porfile;
        }
        
        public function is_liked()
        {
            $user_like = $this->api('/me/likes/' . $this->facebook_config['fb_page_id']);
            return isset($user_like['data']['0']);
        }
        
        public function get_facebook_config($config)
        {
            return $this->facebook_config["{$config}"];
        }
    }
?>